#include "creature.h"

creature::creature(string filename, Arena* level)
{
	mXPosition = 0;
	mYPosition = 0;
	
	m_bitmap = al_load_bitmap(filename.c_str());
	al_convert_mask_to_alpha(m_bitmap, al_map_rgb(255, 0, 255));

	m_width = al_get_bitmap_width(m_bitmap);
	m_height = al_get_bitmap_height(m_bitmap);

	m_width /= 4;
	m_height /= 4;

	map = level;

	m_current_frame = 0;

	bool isRemoved = false;

	m_going_down = true;
	m_going_up = false;
	m_going_right = false;
	m_going_left = false;

}

creature::~creature()
{
	al_destroy_bitmap(m_bitmap);
}

int creature::getX()
{
	return mXPosition;
}

int creature::getY()
{
	return mYPosition;
}

void creature::up()
{
	m_going_down = false;
	m_going_up = true;
	m_going_right = false;
	m_going_left = false;
	if(map->IsEmpty(mXPosition,mYPosition - 1) == '.')
	{
		mYPosition -= 20;
	}
}

void creature::down()
{
	m_going_down = true;
	m_going_up = false;
	m_going_right = false;
	m_going_left = false;
	if(map->IsEmpty(mXPosition, mYPosition + 21) == '.')
	{
		mYPosition += 20;
	}
}

void creature::left()
{
	m_going_down = false;
	m_going_up = false;
	m_going_right = false;
	m_going_left = true;
	if(map->IsEmpty(mXPosition - 1, mYPosition) == '.')
	{
		mXPosition -= 20;
	}
}

void creature::right()
{
	m_going_down = false;
	m_going_up = false;
	m_going_right = true;
	m_going_left = false;
	if(map->IsEmpty(mXPosition + 21, mYPosition) == '.')
	{
		mXPosition += 20;
	}
}

void creature::render()
{
	if(m_going_down)
		al_draw_bitmap_region(m_bitmap, m_current_frame*m_width, 0, m_width, m_height, mXPosition, mYPosition, 0);
	else if(m_going_up)
		al_draw_bitmap_region(m_bitmap, m_current_frame*m_width, 20, m_width, m_height, mXPosition, mYPosition, 0);
	else if(m_going_right)
		al_draw_bitmap_region(m_bitmap, m_current_frame*m_width, 40, m_width, m_height, mXPosition, mYPosition, 0);
	else if(m_going_left)
		al_draw_bitmap_region(m_bitmap, m_current_frame*m_width, 60, m_width, m_height, mXPosition, mYPosition, 0);
}

void creature::nextframe()
{
	if(m_current_frame < 3)
		m_current_frame++;
	else
		m_current_frame = 0;
}